<!DOCTYPE HTML>
<html>
<head>
    <!-- twitter bootstrap CSS stylesheet - included to make things pretty, not needed or used by cornerstone -->
    <link href="../bootstrap.min.css" rel="stylesheet">

    <link href="../cornerstone.min.css" rel="stylesheet">

</head>
<body>
<div class="container">

    <div class="page-header">
        <h1>Example of displaying a DICOM P10 image using Cornerstone</h1>
        <p class="lead">
            Enter a URL for a DICOM P10 object below to view it using cornerstone.
            <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseInfo" aria-expanded="false" aria-controls="collapseInfo">
                Click for more info
            </button>
        </p>


        <ul>
            <!--
            <li>/cornerstoneWADOImageLoader/examples/testImages/ile/gray16.DCM</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/ile/gray8.DCM</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/ile/ybrFull.DCM</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/rle/paletteColor.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/rle/gray16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/rle/rgb.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/rle/multiframe.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.50/ybrFull.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.50/ybrFull422.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.50/gray8.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.50/multiframe.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.51/grayu16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.57/grayu16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.70/gray8.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg.70/grays16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg2000.90/grayu16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg2000.90/grays16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg2000.90/ybrrct.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg2000.91/grays16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg2000.91/grayu16.dcm</li>
            <li>/cornerstoneWADOImageLoader/examples/testImages/jpeg2000.91/ybrict.dcm</li>
            -->
        </ul>
    </div>
    <div class="collapse" id="collapseInfo">
        <p>
            This example illustrates how to use the cornerstoneWADOImageLoader to get a DICOM P10
            SOP instance using HTTP and display it in your web browser using cornerstone.
            Not all transfer syntaxes are currently supported,
            <a href="https://github.com/chafey/cornerstoneWADOImageLoader/blob/master/docs/TransferSyntaxes.md">
                click here for the full list.
            </a>
            For WADO-URI requests,
            you can request that the server return the SOP Instance in explicit little endian by
            appending the following query string to your URL:
            <code>&transferSyntax=1.2.840.10008.1.2.1</code>
        </p>
        <P>
            Use the query string parameter <i>frame</i> to specify which frame to display from a multiframe
            object (defaults to the first frame if not specified). <code>?frame=2</code>
        </P>
        <strong>If you get an HTTP error and your URL is correct, it is probably because the server is not configured to
            allow <a href="http://en.wikipedia.org/wiki/Cross-origin_resource_sharing">Cross Origin Requests</a>.
            Most browsers will allow you to enable cross domain requests via settings or command line switches,
            you can start chrome with the command line switch <code>--disable-web-security</code> to allow cross origin requests.
            See the  <a href="http://enable-cors.org/">Enable CORS site</a> for information about CORS.
        </strong>
        <br>
        <br>
        <p>
            Looking for a CORS proxy?  Try <a href="https://www.npmjs.com/package/corsproxy">CORSProxy</a>
        </p>
        <strong>Use of this example require IE10+ or any other modern browser.</strong>
        <hr>
    </div>
    <div id="loadProgress">Image Load Progress:</div>

    <div class="row">
        <form class="form-horizontal">
            <div class="form-group">
                <label class="control-label col-sm-1" for="wadoURL">URL</label>
                <div class="col-sm-8">
                    <input class="form-control" type="text" id="wadoURL" placeholder="Enter WADO URL" value="http://cornerstonetech.org/images/ClearCanvas/USEcho/IM00001">
                </div>
                <div class="col-sm-3">
                    <button class="form-control" type="button" id="downloadAndView" class="btn btn-primary">Download and View</button>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-2">
                    <button class="form-control" type="button" id="load" class="btn btn-primary">Load</button>
                </div>
                <div class="col-sm-2">
                    <button class="form-control" type="button" id="unload" class="btn btn-primary">Unload</button>
                </div>
                <div class="col-sm-2">
                    <button class="form-control" type="button" id="purge" class="btn btn-primary">Purge Cache</button>
                </div>
            </div>
        </form>
    </div>
    <input type="checkbox" id="toggleModalityLUT">Apply Modality LUT</input>
    <input type="checkbox" id="toggleVOILUT">Apply VOI LUT</input>
    <br>
    <div class="row">
        <div class="col-md-6">
            <div style="width:512px;height:512px;position:relative;color: white;display:inline-block;border-style:solid;border-color:black;"
                 oncontextmenu="return false"
                 class='disable-selection noIbar'
                 unselectable='on'
                 onselectstart='return false;'
                 onmousedown='return false;'>
                <div id="dicomImage"
                     style="width:512px;height:512px;top:0px;left:0px; position:absolute">
                </div>
            </div>
        </div>
        <div class="col-md-6">
            <span>Transfer Syntax: </span><span id="transferSyntax"></span><br>
            <span>SOP Class: </span><span id="sopClass"></span><br>
            <span>Samples Per Pixel: </span><span id="samplesPerPixel"></span><br>
            <span>Photometric Interpretation: </span><span id="photometricInterpretation"></span><br>
            <span>Number Of Frames: </span><span id="numberOfFrames"></span><br>
            <span>Planar Configuration: </span><span id="planarConfiguration"></span><br>
            <span>Rows: </span><span id="rows"></span><br>
            <span>Columns: </span><span id="columns"></span><br>
            <span>Pixel Spacing: </span><span id="pixelSpacing"></span><br>
            <span>Bits Allocated: </span><span id="bitsAllocated"></span><br>
            <span>Bits Stored: </span><span id="bitsStored"></span><br>
            <span>High Bit: </span><span id="highBit"></span><br>
            <span>Pixel Representation: </span><span id="pixelRepresentation"></span><br>
            <span>WindowCenter: </span><span id="windowCenter"></span><br>
            <span>WindowWidth: </span><span id="windowWidth"></span><br>
            <span>RescaleIntercept: </span><span id="rescaleIntercept"></span><br>
            <span>RescaleSlope: </span><span id="rescaleSlope"></span><br>
            <span>Basic Offset Table Entries: </span><span id="basicOffsetTable"></span><br>
            <span>Fragments: </span><span id="fragments"></span><br>
            <span>Max Stored Pixel Value: </span><span id="minStoredPixelValue"></span><br>
            <span>Min Stored Pixel Value: </span><span id="maxStoredPixelValue"></span><br>
            <span>Load Time: </span><span id="loadTime"></span><br>
        </div>
    </div>
</div>
</body>


<!-- jquery - currently a dependency and thus required for using cornerstoneWADOImageLoader -->
<script src="../jquery.min.js"></script>

<!-- bootstrap -->
<script src="../bootstrap.min.js"></script>

<!-- include the cornerstone library -->
<script src="../cornerstone.min.js"></script>
<SCRIPT src="../cornerstoneMath.js"></SCRIPT>
<SCRIPT src="../cornerstoneTools.js"></SCRIPT>

<!-- include the dicomParser library as the WADO image loader depends on it -->
<script src="../dicomParser.min.js"></script>

<!-- OpenJPEG based jpeg 2000 codec -->
<script src="../libopenjpeg.js"></script>

<!-- JPEG-LS codec -->
<script src="../libCharLS.js"></script>

<!-- PDF.js based jpeg 2000 codec -->
<!-- NOTE: do not load the OpenJPEG codec if you use this one -->
<!--<script src="../jpx.min.js"></script>-->

<!-- include the cornerstoneWADOImageLoader library -->
<script src="../../dist/cornerstoneWADOImageLoader.js"></script>

<script src="../dicomfile/uids.js"></script>


<script>

    cornerstoneWADOImageLoader.configure({
        beforeSend: function(xhr) {
            // Add custom headers here (e.g. auth tokens)
            //xhr.setRequestHeader('x-auth-token', 'my auth token');
        }
    });

    var loaded = false;

    function loadAndViewImage(imageId) {
        var element = $('#dicomImage').get(0);
        try {
        var start = new Date().getTime();
            cornerstone.loadAndCacheImage(imageId).then(function(image) {
                console.log(image);
                var viewport = cornerstone.getDefaultViewportForImage(element, image);
                $('#toggleModalityLUT').attr("checked",viewport.modalityLUT !== undefined);
                $('#toggleVOILUT').attr("checked",viewport.voiLUT !== undefined);
                cornerstone.displayImage(element, image, viewport);
                if(loaded === false) {
                    cornerstoneTools.mouseInput.enable(element);
                    cornerstoneTools.mouseWheelInput.enable(element);
                    cornerstoneTools.wwwc.activate(element, 1); // ww/wc is the default tool for left mouse button
                    cornerstoneTools.pan.activate(element, 2); // pan is the default tool for middle mouse button
                    cornerstoneTools.zoom.activate(element, 4); // zoom is the default tool for right mouse button
                    cornerstoneTools.zoomWheel.activate(element); // zoom is the default tool for middle mouse wheel
                    loaded = true;
                }

                function getTransferSyntax() {
                    var value = image.data.string('x00020010');
                    return value + ' [' + uids[value] + ']';
                }

                function getSopClass() {
                    var value = image.data.string('x00080016');
                    return value + ' [' + uids[value] + ']';
                }

                function getPixelRepresentation() {
                    var value = image.data.uint16('x00280103');
                    if(value === undefined) {
                        return;
                    }
                    return value + (value === 0 ? ' (unsigned)' : ' (signed)');
                }

                function getPlanarConfiguration() {
                    var value = image.data.uint16('x00280006');
                    if(value === undefined) {
                        return;
                    }
                    return value + (value === 0 ? ' (pixel)' : ' (plane)');
                }


                $('#transferSyntax').text(getTransferSyntax());
                $('#sopClass').text(getSopClass());
                $('#samplesPerPixel').text(image.data.uint16('x00280002'));
                $('#photometricInterpretation').text(image.data.string('x00280004'));
                $('#numberOfFrames').text(image.data.string('x00280008'));
                $('#planarConfiguration').text(getPlanarConfiguration());
                $('#rows').text(image.data.uint16('x00280010'));
                $('#columns').text(image.data.uint16('x00280011'));
                $('#pixelSpacing').text(image.data.string('x00280030'));
                $('#bitsAllocated').text(image.data.uint16('x00280100'));
                $('#bitsStored').text(image.data.uint16('x00280101'));
                $('#highBit').text(image.data.uint16('x00280102'));
                $('#pixelRepresentation').text(getPixelRepresentation());
                $('#windowCenter').text(image.data.string('x00281050'));
                $('#windowWidth').text(image.data.string('x00281051'));
                $('#rescaleIntercept').text(image.data.string('x00281052'));
                $('#rescaleSlope').text(image.data.string('x00281053'));
                $('#basicOffsetTable').text(image.data.elements.x7fe00010.basicOffsetTable ? image.data.elements.x7fe00010.basicOffsetTable.length : '');
                $('#fragments').text(image.data.elements.x7fe00010.fragments ? image.data.elements.x7fe00010.fragments.length : '');
                $('#minStoredPixelValue').text(image.minPixelValue);
                $('#maxStoredPixelValue').text(image.maxPixelValue);
                var end = new Date().getTime();
                var time = end - start;
                $('#loadTime').text(time + "ms");

            }, function(err) {
                alert(err);
            });
        }
        catch(err) {
            alert(err);
        }
    }

    function downloadAndView()
    {
        var url = $('#wadoURL').val();

        // prefix the url with wadouri: so cornerstone can find the image loader
        url = "wadouri:" + url;


        // image enable the dicomImage element and activate a few tools
        loadAndViewImage(url);
    }

    $(cornerstone).bind('CornerstoneImageLoadProgress', function(eventData) {
        $('#loadProgress').text('Image Load Progress: ' + eventData.percentComplete + "%");
    });

    function getUrlWithoutFrame() {
        var url = $('#wadoURL').val();
        var frameIndex = url.indexOf('frame=');
        if(frameIndex !== -1) {
            url = url.substr(0, frameIndex-1);
        }
        return url;
    }

    $(document).ready(function() {

        var element = $('#dicomImage').get(0);
        cornerstone.enable(element);

        $('#downloadAndView').click(function(e) {
            downloadAndView();
        });
        $('#load').click(function(e) {
            var url = getUrlWithoutFrame();
            cornerstoneWADOImageLoader.dataSetCacheManager.load(url);
        });
        $('#unload').click(function(e) {
            var url = getUrlWithoutFrame();
            cornerstoneWADOImageLoader.dataSetCacheManager.unload(url);
        });

        $('#purge').click(function(e) {
            cornerstone.imageCache.purgeCache();
        });

        $('form').submit(function() {
            downloadAndView();
            return false;
        });

        $('#toggleModalityLUT').on('click', function() {
            var applyModalityLUT = $('#toggleModalityLUT').is(":checked");
            console.log('applyModalityLUT=', applyModalityLUT);
            var image = cornerstone.getImage(element);
            var viewport = cornerstone.getViewport(element);
            if(applyModalityLUT) {
                viewport.modalityLUT = image.modalityLUT;
            } else {
                viewport.modalityLUT = undefined;
            }
            cornerstone.setViewport(element, viewport);
        });

        $('#toggleVOILUT').on('click', function() {
            var applyVOILUT = $('#toggleVOILUT').is(":checked");
            console.log('applyVOILUT=', applyVOILUT);
            var image = cornerstone.getImage(element);
            var viewport = cornerstone.getViewport(element);
            if(applyVOILUT) {
                viewport.voiLUT = image.voiLUT;
            } else {
                viewport.voiLUT = undefined;
            }
            cornerstone.setViewport(element, viewport);
        });


    });

</script>
</html>
